<template>
  <el-dialog title="新增员工" :visible="showDialog" :close-on-click-modal="false" @close="hideDialog" @click.native="isShow=false">
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- 此处为选择框 所以选择后不能再修改值 应当用:value绑定 -->
        <el-input :value="form.departmentName" style="width:50%" placeholder="请选择部门" @click.native.stop="getDepartments" />
        <div v-show="isShow" class="tree-box">
          <el-tree v-loading="isLoding" :data="departmentList" :props="defaultProps" @node-click="handleNodeClick" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template #footer>
      <el-button @click="hideDialog">取消</el-button>
      <el-button type="primary" @click="btn">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 获取部门列表
import { getDepartmentsApi } from '@/api/departments'
// 导入枚举
import Enum from '@/constant/employees'
// 递归生成树状数据方法
import { scree } from '@/utils'
import { addEmployeesUserApi } from '@/api/employees'
import { Message } from 'element-ui'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '员工姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '员工姓名长度要求1-4字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '员工手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '员工聘用形式不能为空', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '员工工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '员工部门名称不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '员工入职日期不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '员工转正日期不能为空', trigger: 'blur' }
        ]
      },
      // 聘用方式数组
      options: [],
      // 部门列表数据
      departmentList: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 树状数据显示隐藏
      isShow: false,
      // 加载状态
      isLoding: false
    }
  },
  created() {
    this.options = Enum.hireType
  },
  mounted() {
    // 定时器
    // setInterval(() => {
    //   this.btn()
    // }, 100)
  },
  methods: {
    /**
     * 点击确定按钮
     */
    btn() {
      // 1.兜底效验
      this.$refs.form.validate(async(key) => {
        if (!key) return
        // 2.调用接口
        await addEmployeesUserApi(this.form)
        // 3.提示成功
        Message.success('添加成功')
        // 4.关闭弹窗
        this.hideDialog()
        // 5.从新渲染页面
        this.$emit('add')
      })
    },
    /**
     * 树状图点击事件
     */
    handleNodeClick(val) {
      if (val.children.length > 0) return
      this.form.departmentName = val.name
    },
    /**
     * 获取部门列表 且转换为树状数据
     */
    async getDepartments() {
      this.isShow = !this.isShow
      this.isLoding = true
      if (!this.isShow) return

      const { data } = await getDepartmentsApi()
      this.departmentList = scree(data.depts, '')
      this.isLoding = false
    },
    /**
     * 关闭与取消按钮事件
     */
    hideDialog() {
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang='scss' scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
